package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrServiceImpl attrService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> getAttrGroupByCategory(Long cid) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", cid);
        List<AttrGroupEntity> attrGroupEntity = this.list(wrapper);
        return attrGroupEntity;
    }

    @Override
    public List<AttrGroupEntity> getAttrGroutsByCid(Long cid) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",cid);
        List<AttrGroupEntity> groupEntities = this.list(wrapper);

        if (Collections.isEmpty(groupEntities)){
            return null;
        }

        groupEntities.forEach(g -> {
            Long id = g.getId();
            QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("group_id",id).eq("type", 1);
            List<AttrEntity> attrEntities = attrService.list(queryWrapper);
            g.setAttrEntities(attrEntities);
        });

        return groupEntities;
    }

}